package org.asdmp.restday.controller;

import java.io.IOException;
import java.util.Set;

import javax.servlet.Servlet;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.asdmp.restday.service.ParallelService;

/**
 * Servlet implementation class CompleteTaskController
 */
@WebServlet("/controller/redParallelCompleteTaskController")
public class RedParallelCompleteTaskController extends HttpServlet implements Servlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public RedParallelCompleteTaskController() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request,response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setCharacterEncoding("UTF-8");
		request.setCharacterEncoding("UTF-8");
		ParallelService service = new ParallelService();
		String paratask = request.getParameter("paratask");
		String fromnodes = request.getParameter("nodes");
		String processInstanceId = request.getParameter("processInstanceId");
		try {
			service.completeTask(processInstanceId,paratask);
			service.print("Jerry", "complete");
			Set<String> names = service.getTasks(processInstanceId); 
			String namesStr = "";
			for (String name:names){
				namesStr += name + ",";
			}
			if (names.size() > 0) namesStr = namesStr.substring(0, namesStr.length()-1);
			if (namesStr == null || "".equals(namesStr)) namesStr="[结束]";
			
			request.setAttribute("fromnodes",fromnodes);
			request.setAttribute("nodes",namesStr);
			request.setAttribute("responseText","Jerry========Complete");
			System.out.println("fromnodes========"+fromnodes);
			System.out.println("nodes========"+namesStr);
			System.out.println("Jerry========Complete");
		} catch(Exception e){
		}
		request.getRequestDispatcher("../jsp/redparallelconsole.jsp").forward(request, response); 
	}

}

